<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Usermodel extends CI_Model {

    public $table = 'users';
    public $primary = 'user_id';

    // will remove
    function addUser() {
      //$query = $this->db->query("ALTER TABLE `users` ADD `point` INT( 11 ) NOT NULL");
       
        return;
        for ($i = 7100; $i <= 10000; $i++) {
            $data = array(
                'email' => "tk{$i}@adshop.com",
                'password' => md5('123456')
            );
            //INSERT INTO `project2`.`users` (`email`, `password`) VALUES ('tk1@adshop', md5('123456'));
            $this->db->insert($this->table, $data);
        }
    }

    function add($data) {
        $this->db->insert($this->table, $data);
        return $this->db->insert_id();
    }

    function update($user_id, $data) {
        $this->db->where($this->primary, $user_id);
        return $this->db->update($this->table, $data);
    }

    public function delete($user_id) {
        $this->db->where($this->primary, $user_id);
        return $this->db->delete($this->table);
    }

    function getUserById($user_id) {
        $query = $this->db->query("SELECT * FROM  {$this->table} 
    							WHERE {$this->primary} = {$user_id}");
        return $query->row_array();
    }

    function getAllUserExceptCurrent($user_id) {
        $query = $this->db->query("SELECT * FROM  {$this->table} 
    							WHERE {$this->primary} <> {$user_id}");
        return $query->result_array();
    }

    function getNameAllUserExceptCurrent($user_id) {
        $query = $this->db->query("SELECT user_id AS id, real_name AS name FROM  {$this->table} 
    							WHERE {$this->primary} <> {$user_id}");
        return $query->result_array();
    }

    function getAllUser($user_id) {
        $query = $this->db->query("SELECT * FROM  {$this->table}");
        return $query->result_array();
    }

    function login($email, $password) {
        $sql = 'SELECT *
    			FROM `users` WHERE `email` = ? AND `password` = ? LIMIT 1';
        $query = $this->db->query($sql, array($email, MD5($password)));
        //echo $this->db->last_query();die;

        if ($query->num_rows() == 1) {
            return $query->row_array();
        } else {
            return false;
        }
    }

}